package com.pajk.user.dal.index;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.SQLException;

/**
 * Created by peide.zhang on 4/30/14.
 */
public class DBIndex  <KeyType extends Serializable, ValueType> implements Index<KeyType, ValueType>{

    private static final Logger Log = LoggerFactory.getLogger(DBIndex.class);

    private SqlSessionTemplate sqlSessionTemplate;
    private String mapperKey;

    @Override
    public ValueType get(KeyType key) {
        return null;
    }

    @Override
    public void put(KeyType key, ValueType val) {
        try {
            sqlSessionTemplate.insert(key.toString(),val);
        } catch(RuntimeException re){
            Log.error("RuntimeException catched.", re);
        }catch(Throwable t){
            Log.error("RuntimeException catched.", t);
        }
    }

    @Override
    public void remove(KeyType key) {

    }

    @Override
    public void remove(KeyType key, Object val) {
        try {
            sqlSessionTemplate.delete(key.toString(), val);
        } catch(RuntimeException re){
            Log.error("RuntimeException catched.", re);
        }catch(Throwable t){
            Log.error("RuntimeException catched.", t);
        }
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
}
